package com.biostime.qdingding.modules.qd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Courseserial;
import com.biostime.qdingding.modules.qd.service.CourseserialService;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 课程系列Controller
 * @author king
 * @version 2015-11-05
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/courseserial")
public class CourseserialController extends BaseController {

	@Autowired
	private CourseserialService courseserialService;
	
	@ModelAttribute
	public Courseserial get(@RequestParam(required=false) String id) {
		Courseserial entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = courseserialService.get(id);
		}
		if (entity == null){
			entity = new Courseserial();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:courseserial:view")
	@RequestMapping(value = {"list", ""})
	public String list(Courseserial courseserial, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Courseserial> page = courseserialService.findPage(new Page<Courseserial>(request, response), courseserial); 
		model.addAttribute("page", page);
		return "modules/qd/courseserialList";
	}

	@RequiresPermissions("qd:courseserial:view")
	@RequestMapping(value = "form")
	public String form(Courseserial courseserial, Model model) {
		model.addAttribute("courseserial", courseserial);
		return "modules/qd/courseserialForm";
	}

	@RequiresPermissions(value={"qd:courseserial:edit","qd:courseserial:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Courseserial courseserial, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, courseserial)){
			return form(courseserial, model);
		}
		
		courseserial.setCenter(UserUtils.getUser().getCenter());
		
		courseserialService.save(courseserial);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/courseserial/?repage";
	}
	
	@RequiresPermissions("qd:courseserial:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Courseserial courseserial, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				courseserialService.delete(new Courseserial(id));
			}
		}else{
			courseserialService.delete(courseserial);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/courseserial/?repage";
	}

}